package service

import (
	"errors"
	uuid "github.com/satori/go.uuid"
	"go_shengshu_admin/model"
	"go_shengshu_admin/query"
	"go_shengshu_admin/repository"
)

type ProductService struct {
	Repo *repository.ProductRepository
}

type ProductSrv interface {
	List(req *query.ListQuery) (products []*model.Product, err error)
	GetTotal() (total int64, err error)
	GetProduct(product model.Product) (*model.Product, error)
	Exist(product model.Product) *model.Product
	ExistById(id string) *model.Product
	Add(product model.Product) (*model.Product, error)
	Edit(product model.Product) (bool, error)
	Delete(id string) (bool, error)
}

func (srv *ProductService) List(req *query.ListQuery) (products []*model.Product, err error) {
	return srv.Repo.List(req)
}

func (srv *ProductService) GetTotal() (total int64, err error) {
	return srv.Repo.GetTotal()
}

func (srv *ProductService) GetProduct(product model.Product) (*model.Product, error) {
	return srv.Repo.GetProduct(product)
}

func (srv *ProductService) Exist(product model.Product) *model.Product {
	return srv.Repo.Exist(product)
}

func (srv *ProductService) ExistById(id string) *model.Product {
	return srv.Repo.ExistById(id)
}

func (srv *ProductService) Add(product model.Product) (*model.Product, error) {
	product.ProductId = uuid.NewV4().String()
	product.IsDeleted = false
	return srv.Repo.Add(product)
}

func (srv *ProductService) Edit(product model.Product) (bool, error) {
	if product.ProductId == "" {
		return false, errors.New("参数错误")
	}
	if srv.Repo.ExistById(product.ProductId) != nil {
		return false, errors.New("产品不存在")
	}
	return srv.Repo.Edit(product)
}

func (srv *ProductService) Delete(id string) (bool, error) {
	if id == "" {
		return false, errors.New("参数错误")
	}
	if srv.Repo.ExistById(id) != nil {
		return false, errors.New("产品不存在")
	}
	return srv.Repo.Delete(id)
}
